package mhl.service;

import mhl.DAO.billDao;
import mhl.domain.bill;
import mhl.domain.menu;

import java.util.List;
import java.util.UUID;

public class billService {
    private billDao billdao = new billDao();
    private menuService menuservice = new menuService();
    private diningTableService diningtableservice = new diningTableService();
    public boolean updateBill(int menuId,int nums,int diningTableId){
        String sql = "insert into bill values(null,?,?,?,?,?,now(),'未结账')";
        String billId = UUID.randomUUID().toString();
        double price = (double)menuservice.getPricebyId(menuId);
        int effectBill = billdao.updateTable(sql, billId, menuId, nums, price * nums, diningTableId);
        int effectDiningTable = diningtableservice.updateDiningTableStatus(diningTableId);
        return effectBill > 0 && effectDiningTable > 0;

    }

    public List<bill> listBill(){
        return billdao.getMany("select * from bill", mhl.domain.bill.class);
    }

    public boolean hasPayBill(int diningTableId){
        String sql = "select * from bill where diningTableId = ? and status = '未结账' LIMIT 0,1 ";
        bill single = billdao.getSingle(sql, bill.class, diningTableId);
        return single != null;

    }

    public boolean updateBill(int diningTableId,String payMode){
        String sql = "update bill set status = ? where diningTableId = ? and status = '未结账'";
        return billdao.updateTable(sql, payMode,diningTableId) > 0;

    }



}
